﻿using System.Drawing;
using System.Windows.Forms;

namespace Test30.Tools.DataView
{
    public partial class ViewControl1 : UserControl
    {
        public MapHandler MapHandler { get; private set; }
        public ViewControl1()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        public void SetMap()
        {
            MapHandler = new MapHandler();
            Refresh();
        }

        public void Next()
        {
            MapHandler.MakeCaverns();
            Refresh();
        }

        public override void Refresh()
        {
            Invalidate();
        }

        public void UpdateCount()
        {
            MapHandler.GeneratorCount();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.ScaleTransform(1.0F, -1.0F);
            e.Graphics.TranslateTransform(0.0F, -(float)Height);

            base.OnPaint(e);
            if (MapHandler != null)
            {
                for (int x = 0; x < MapHandler.MapWidth; x++)
                {
                    for (int y = 0; y < MapHandler.MapHeight; y++)
                    {
                        Rectangle rect = new Rectangle(x * TileSize, y * TileSize, TileSize, TileSize);
                        if (e.ClipRectangle.IntersectsWith(rect))
                        {
                            Brush brush = Brushes.DarkBlue;
                            if (MapHandler.Map[x, y].Type == Engine.TileType.Wall) brush = Brushes.DarkGray;
                            if (MapHandler.Map[x, y].Type == Engine.TileType.StairsUp) brush = Brushes.OrangeRed;
                            if (MapHandler.Map[x, y].Type == Engine.TileType.StairsDown) brush = Brushes.DeepSkyBlue;

                            e.Graphics.FillRectangle(brush, rect);
                            e.Graphics.DrawRectangle(Line, rect);
                        }
                    }
                }
            }
        }
        private static readonly Pen Line = new Pen(Color.FromArgb(32, 0, 0, 0));
        private const int TileSize = 20;
        
    }
}
